﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  JoinNowVoiceActivity.cs
//    
// Abstract:
//
//  Activity for sending the other party in an voice call
//  to a conference call or live meeting.
//  
//--

using System;
using System.Diagnostics;
using System.Workflow.ComponentModel;
using Microsoft.Rtc.Workflow.Activities;
using Microsoft.Rtc.Collaboration;

namespace Microsoft.EnhancedReminders.Dispatcher.Workflow
{
    /// <summary>
    /// Workflow activity for joining a conference via voice. This works
    /// by first joining the conference or live meeting, and then inviting the other
    /// party to the meeting. When this is done, the current account leaves the meeting.
    /// 
    /// Currently the bot account will attempt to join the conference or meeting
    /// as itself.  However, if the Dispatcher is running as an application it attempts to 
    /// impersonate the user before joining the conference.
    /// </summary>
    public partial class JoinNowVoiceActivity : JoinNowActivityBase
    {
        /// <summary>
        /// The call that is being transferred to the conference.
        /// </summary>
        private static readonly DependencyProperty CallProperty =
            DependencyProperty.Register("Call", typeof(Call), typeof(JoinNowVoiceActivity));

        private static readonly InstanceDependencyProperty NotificationProperty =
            InstanceDependencyProperty.Register("Notification", typeof(CalendarNotificationItem), typeof(JoinNowVoiceActivity));

        private Guid _subscriptionId;

        /// <summary>
        /// ctor.
        /// </summary>
        public JoinNowVoiceActivity()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The call to be transferred.
        /// </summary>
        public override Call Call
        {
            get
            {
                return (Call)this.GetValue(CallProperty);
            }
            set
            {
                this.SetValue(CallProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the address of the conference to join.
        /// </summary>
        public override CalendarNotificationItem Notification
        {
            get
            {
                return InstanceDependencyHelper.GetValue<JoinNowVoiceActivity>(this, NotificationProperty) as CalendarNotificationItem;
            }
            set
            {
                InstanceDependencyHelper.SetValue<JoinNowVoiceActivity>(this, NotificationProperty, value);
            }
        }

        /// <summary>
        /// Subscribes the activity to the JoinNowVoiceService.
        /// </summary>
        /// <param name="parentContext">The parent context.</param>
        protected override void SubscribeToJoinNowService(ActivityExecutionContext parentContext)
        {
            JoinNowVoiceService joinNowService = parentContext.GetService<JoinNowVoiceService>();
            _subscriptionId = joinNowService.RegisterJoinNowOperation(QueueName, Call, Notification);
        }

        /// <summary>
        /// Unsubscribes the activity from the JoinNowVoiceService.
        /// </summary>
        /// <param name="parentContext">The parent context.</param>
        protected override void UnsubscribeFromJoinNowService(ActivityExecutionContext parentContext)
        {
            JoinNowVoiceService joinNowService = parentContext.GetService<JoinNowVoiceService>();
            Debug.Assert(joinNowService != null, "joinNowService != null");

            joinNowService.UnregisterJoinNowOperation(_subscriptionId);
            _subscriptionId = Guid.Empty;
        }
    }
}
